package com.ncloud.crm.service.impl;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ncloud.aaaa.service.UserService;
import com.ncloud.crm.common.Constant;
import com.ncloud.crm.entity.BsCust;
import com.ncloud.crm.mapper.BsCustMapper;
import com.ncloud.crm.service.BsCustService;
import com.ncloud.msg.dubboservice.MailDubboService;

@Service
public class BsCustServiceImpl implements BsCustService {
	
	private static final Logger logger = LogManager.getLogger(BsCustServiceImpl.class);
	@Autowired
	private BsCustMapper custMapper;
	@Reference
	private UserService userService;
	@Reference
	private MailDubboService mailService;
	
	@Override
	public int insertBsCust(BsCust cust) {
		Long custId = cust.getCustId();
		cust.setCustState(Constant.CUST_STATE_NORMAL);
		custMapper.insertBsCust(cust);
		//添加销售和客户的关系
		BsCust relCust = getChnlCustByCustIdAndUserId(custId,cust.getCreateUser());
		if(relCust == null || relCust.getCustId()==null)
			createChnlCustRel(custId,cust.getCreateUser());
		return 1;
	}



	@Override
	public int updateBsCust(BsCust cust) {
		return custMapper.updateBsCust(cust);
	}

	@Override
	public BsCust getByCompanyCode(String companyCode) {
		return custMapper.getByCompanyCode(companyCode);
	}

	@Override
	public int createChnlCustRel(Long custId, Long userId) {
		return custMapper.createChnlCustRel(custId,userId);
	}

	@Override
	public BsCust getChnlCustByCustIdAndUserId(Long custId, Long userId) {
		return custMapper.getChnlCustByCustIdAndUserId(custId,userId);
	}

	@Override
	public BsCust getCustInfoById(Long custId) {
		return custMapper.getCustInfoById(custId);
	}
}
